/**
 * 
 */
package com.mvc.controller.fileManager;

import java.io.File;
import java.text.ParseException;
import java.util.Date;

import javax.servlet.http.HttpServletRequest;

import org.huangqj.consant.GlobalConstant;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import com.mvc.model.base.Json;
import com.mvc.model.healthmanager.archive.FileInfo;
import com.mvc.service.upgrade.FileInfoServiceI;

/**
 * @author qingjie
 *
 */
@Controller
@RequestMapping("/upload")
public class FileUpLoadController {
	

	@Autowired
	private FileInfoServiceI fis;
	
	/**
	 * 保存或更改健康活动
	 * @param session
	 * @param pActive
	 * @return
	 * @throws ParseException 
	 */
	@RequestMapping("/{tname}/{tid}/{type}")
	@ResponseBody
	public Json addActive(@RequestParam(value = "sourceFile", required = false) MultipartFile[] files,
			HttpServletRequest request,MultipartHttpServletRequest Mrequest,
			@PathVariable String tname,@PathVariable Long tid,@PathVariable int type){
		Json json = new Json();
		
		try {
		
			//判断file数组不能为空并且长度大于0  
	        if(files!=null&&files.length>0){  
	            //循环获取file数组中得文件  
	            for(int i = 0;i<files.length;i++){  
	                MultipartFile file = files[i];   
	                // 判断文件是否为空  
	                if (!file.isEmpty()) {  
	                	FileInfo fi = new FileInfo();
	                	String suffix = file.getOriginalFilename().substring(file.getOriginalFilename().lastIndexOf(".")+1);
	                	String newName = new Date().getTime()+"."+suffix;
	                	// 文件保存路径 
	                	String filePath = request.getSession().getServletContext().getRealPath("")+"\\files\\"+newName;    
	                	switch (type) {
	                	case 1:
	                		fis.uploadByTable(tid, tname, "/files/"+newName);
	                		break;
						case 2:
							fi.setFilename(file.getOriginalFilename());
		                	fi.setNewFilename(newName);
		                	fi.setPath(filePath);
		                	fi.setSize(file.getSize()+"");
		                	fi.settId(tid+"");
		                	fi.settName(tname);
		                	fi.setSuffix(suffix);
		                	fis.saveOrUpdate(fi);
							break;

						default:
							break;
						}
	                	
	                    try {  
	                        // 转存文件  
	                        File targetFile = new File(filePath);  
	            	        if(!targetFile.exists()){  
	            	            targetFile.mkdirs();  
	            	        }
	                        file.transferTo(targetFile); 
	                        json.setSuccess(true);
	            			json.setMsg("保存成功！");
	                    } catch (Exception e) {  
	                        e.printStackTrace();  
	                    }  
	                }  
	            }
	           
	        }
	        
		} catch(Exception e){
			json.setSuccess(false);
			json.setMsg("保存失败，请联系管理员！");
			e.printStackTrace();
		}
		return json;	
	}

}
